package design.leetcode_155;

import java.util.ArrayDeque;
import java.util.Deque;

public class OnlyStackSolution {
    class MinStack {
        Deque<Integer> stack;
        int min;

        /**
         * initialize your data structure here.
         */
        public MinStack() {
            stack = new ArrayDeque<>();
            min = Integer.MAX_VALUE;
        }

        public void push(int val) {
            if (val <= min) {
                stack.push(min);
                min = val;
            }
            stack.push(val);
        }

        public void pop() {
            if (!stack.isEmpty() && stack.pop() == min) {
                min = stack.pop();
            }
        }

        public int top() {
            if (!stack.isEmpty()) {
                return stack.peek();
            }
            throw new RuntimeException("栈为空! 非法操作!");
        }

        public int getMin() {
            return min;
        }
    }
}
